<?php
/*
Plugin Name: Visual Composer Hello World
Plugin URI: http://www.facebook.com/NguyenDuyPhong/
Description: Extend Visual Composer, shortcodes, enqueue js script, css.
Version: 1.0
Author: PhongND
Author URI: http://www.facebook.com/NguyenDuyPhong/
License: GPLv2 or later
*/


// don't load directly
if (!defined('ABSPATH')) die('-1');

class VCHelloWorldClass {
    function __construct() {
        // We safely integrate with VC with this hook
        add_action( 'init', array( $this, 'integrateWithVC' ) );
 
        // Use this when creating a shortcode addon
        add_shortcode( 'vc_helloworld', array( $this, 'render_vc_helloworld' ) );

        // Register CSS and JS
        add_action( 'wp_enqueue_scripts', array( $this, 'loadCssAndJs' ) );
    }
 
    public function integrateWithVC() {
        // Check if Visual Composer is installed
        if ( ! defined( 'WPB_VC_VERSION' ) ) {
            // Display notice that Visual Compser is required
            add_action('admin_notices', array( $this, 'showVcVersionNotice' ));
            return;
        }
 
        /*
        Add your Visual Composer logic here.
        Lets call vc_map function to "register" our custom shortcode within Visual Composer interface.

        More info: http://kb.wpbakery.com/index.php?title=Vc_map
        */
        vc_map( array(
            "name" => __("VC Hello World Shortcode", 'domain_helloworld'),
            "description" => __("VC Hello World description text", 'domain_helloworld'),
            "base" => "vc_helloworld",
            "class" => "",
            "controls" => "full",
            "icon" => plugins_url('assets/asterisk_yellow.png', __FILE__), // or css class name which you can reffer in your css file later. Example: "domain_helloworld_my_class"
            "category" => __('Content', 'js_composer'),
            // 'admin_enqueue_js' => array(plugins_url('assets/hw_cssjs.js', __FILE__)), // js for backend editor
            //'admin_enqueue_css' => array(plugins_url('assets/hw_cssjs_admin.css', __FILE__)), // css for VC backend editor
            "params" => array(
                array(
                  "type" => "textfield",
                  "holder" => "div",
                  "class" => "",
                  "heading" => __("Title", 'domain_helloworld'),
                  "param_name" => "title",
                  "value" => __("title_default", 'domain_helloworld'), //default text value.
                  "description" => __("title.....", 'domain_helloworld')
              ),
              array(
                  "type" => "colorpicker",
                  "holder" => "div",
                  "class" => "",
                  "heading" => __("Title color", 'domain_helloworld'),
                  "param_name" => "color",
                  "value" => '#FF0000', //Default Red color.
                  "description" => __("Choose text color", 'domain_helloworld')
              ),
              array(
                  "type" => "textarea_html",
                  "holder" => "div",
                  "class" => "",
                  "heading" => __("Content", 'domain_helloworld'),
                  "param_name" => "content",
                  "value" => __("<p>content_default.</p>", 'domain_helloworld'),
                  "description" => __("Enter your content.", 'domain_helloworld')
              ),
            )
        ) );
    }
    
    /*
    Shortcode logic how it should be rendered
    */
    public function render_vc_helloworld( $atts, $content = null ) {
      extract( shortcode_atts( array(
        'title' => 'something',
        'color' => '#FF0000'
      ), $atts ) );
      $content = wpb_js_remove_wpautop($content, true); // fix unclosed/unwanted paragraph tags in $content
     
      $output = "<div style='color:{$color};' data-title='${title}'>".  $title.': '.$content. "</div>";
      return $output;
    }

    /*
    Load plugin css and javascript files which you may need on front end of your site
    */
    public function loadCssAndJs() {
      wp_register_style( 'vc_helloworld_style', plugins_url('assets/hw_cssjs.css', __FILE__) );
      wp_enqueue_style( 'vc_helloworld_style' );
	  
      // enqueue js on front end
      // wp_enqueue_script( 'vc_helloworld_js', plugins_url('assets/hw_cssjs.js', __FILE__), array('jquery') );
    }

    /*
    Show notice if your plugin is activated but Visual Composer is not
    */
    public function showVcVersionNotice() {
        $plugin_data = get_plugin_data(__FILE__);
        echo '
        <div class="updated">
          <p>'.sprintf(__('<strong>%s</strong> requires <strong><a href="http://bit.ly/vcomposer" target="_blank">Visual Composer</a></strong> plugin to be installed and activated on your site.', 'domain_helloworld'), $plugin_data['Name']).'</p>
        </div>';
    }
}
// Finally initialize code
new VCHelloWorldClass();

